public class Solution02 {
	public TreeNode convertBST(TreeNode root) {
		if(root == null){
			return null;
		}

		Deque<TreeNode> stack = new ArrayDeque<>();
		TreeNode curNode = root;
		int sum = 0;
		while(curNode != null || !stack.isEmpty()){
			while(curNode != null){
				stack.offerLast(curNode);
				curNode = curNode.right;
			}
			curNode = stack.pollLast();
			curNode.val += sum;
			sum = curNode.val;
			curNode = curNode.left;
		}

		return root;
	}
}
